﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="BaseController.cs" company="Spenta Consulting">
//   (c) Copyright 2011
// </copyright>
// <summary>
//   Base class for all StreetCare Controllers. It contains all these common elements that can be used in any controller context
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Spenta.StreetCare.MVC.Controllers
{
    #region Using statements

    using System;
    using System.Linq;
    using System.Web.Mvc;
    using Core.Models;

    using Spenta.StreetCare.Core.IoC;

    #endregion

    /// <summary>
    /// Base class for all StreetCare Controllers. It contains all these common elements that can be used in any controller context
    /// </summary>
    public class BaseController : Controller
    {
        /// <summary>
        /// Private store for the StreetCare entities object
        /// </summary>
        private StreetCareEntities entities;

        #region Properties

        /// <summary>
        /// Gets a reference to the customer related with the domain being navigated
        /// </summary>
        protected Customer Customer { get; private set; }

        /// <summary>
        /// Gets a reference to the StreetCare entities object
        /// </summary>
        protected StreetCareEntities Entities
        {
            get
            {
                return this.entities ?? (this.entities = new StreetCareEntities(IoCHelper.ConfigurationBag.GetConnectionString("StreetCareEntities")));
            }
        }

        #endregion

        /// <summary>
        /// Initializes data that might not be available when the constructor is called.
        /// </summary>
        /// <param name="requestContext">The HTTP context and route data.</param>
        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            base.Initialize(requestContext);

            // TODO: handle error if no customer is found
            if (Request.Url != null)
            {
                string hostname = Request.Url.DnsSafeHost;

                this.Customer = this.Entities.Customers.Where(c => c.CustomerHostname.Equals(hostname)).FirstOrDefault();

                // If no customer has been found we need to create it
                if (this.Customer == null)
                {
                    this.Customer = new Customer
                        {
                            CustomerName = "Default customer",
                            CustomerHostname = Request.Url.DnsSafeHost,
                            CustomerLogoUrl = "/images/default/streetscenelogo.png",
                            CustomerMapCredentials = string.Empty
                        };
                    this.Entities.AddObject("Customers", this.Customer);
                    this.Entities.SaveChanges();
                }

                // Finally we add the customer in the RouteData store to retrieve it anywhere within the MVC website scope
                requestContext.RouteData.Values.Add("Customer", this.Customer);
            }
            else
            {
                throw new ApplicationException("Request is null. Cannot continue.");
            }
        }
    }
}
